﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevWeb.Biztalk.DeploymentSettings.Data.CodeFirst.Configurations;

namespace DevWeb.Biztalk.DeploymentSettings.Data.CodeFirst.Context
{
    public class DeploymentSettingsContext: DbContext
    {
        public DbSet<Integration> Integrations { get; set; }
        public DbSet<IntegrationVersion> IntegrationVersions { get; set; }
        public DbSet<TargetEnvironment> TargetEnvironments { get; set; }

        public DbSet<IntegrationVersionParameter> IntegrationVersionParameters { get; set; }
        public DbSet<IntegrationVersionParameterValue> IntegrationVersionParameterValues { get; set; }
        public DbSet<DefaultSettingParameter> DefaultSettingParameters { get; set; }
        public DbSet<DefaultSettingParameterValue> DefaultSettingParameterValues { get; set; }
        public DbSet<DeploymentLog> DeploymentLogs { get; set; }

      

        public DeploymentSettingsContext()
            : base("DeploymentSettingsConnection")
        {
            Init();
        }

        private void Init()
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = true;
            
           
            if (!base.Database.Exists())
            {
                base.Database.CreateIfNotExists();

                SetupSeed();
            }
            SetupSeed();
        }

        private void SetupSeed()
        {
            CheckBasicEnvironments("DEV");
            CheckBasicEnvironments("TST");
            CheckBasicEnvironments("ACC");
            CheckBasicEnvironments("PRD");
            SaveChanges();
        }

        private void CheckBasicEnvironments(string environment)
        {

            try
            {
                var devenv = TargetEnvironments.SingleOrDefault(p => p.Name.Equals(environment.ToUpper()));

                if (devenv == null)
                {
                    var newenvironment = new TargetEnvironment()
                    {
                        Name = environment,
                        Description = "Initial Created " + environment
                    };

                    TargetEnvironments.Add(newenvironment);
                }
            }
            catch (Exception exception)
            {
                
                Trace.Write(exception);
            }
           
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.Configuration.AutoDetectChangesEnabled = true;
            modelBuilder.Configurations.Add(new IntegrationConfiguration());
            modelBuilder.Configurations.Add(new IntegrationVersionConfiguration());
            modelBuilder.Configurations.Add(new TargetEnvironmentConfiguration());
            modelBuilder.Configurations.Add(new DefaultSettingParameterConfiguration());
            modelBuilder.Configurations.Add(new IntegrationVersionParameterConfiguration());
            modelBuilder.Configurations.Add(new IntegrationVersionParameterValueConfiguration());
            modelBuilder.Configurations.Add(new DeploymentLogConfiguration());
           
           
            
            base.OnModelCreating(modelBuilder);
        }

        protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
        {
            string type = entityEntry.Entity.GetType().FullName;

            Trace.WriteLine(entityEntry);
            return base.ValidateEntity(entityEntry, items);
        }
    }

   
}
